package com.xunyin.modules.index.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunyin.modules.index.service.IndexService;
import com.xunyin.modules.index.service.dto.IndexDto;
import com.xunyin.modules.mtorder.service.mapper.MtOrderMapper;
import com.xunyin.modules.shareorder.domain.TShareOrder;
import com.xunyin.modules.shareorder.service.TShareOrderService;
import com.xunyin.modules.shareorder.service.mapper.TShareOrderMapper;
import com.xunyin.modules.user.service.YxUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台首页相关接口
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IndexServiceImpl implements IndexService {

    @Autowired
    private YxUserService yxUserService;

    @Autowired
    private TShareOrderService tShareOrderService;

    @Autowired
    private TShareOrderMapper tShareOrderMapper;

    @Autowired
    private MtOrderMapper mtOrderMapper;

    /**
     * 首页订单/用户等统计
     *
     * @return IndexDto
     */
    @Override
    public IndexDto getIndexDataStat(Integer storeId) {

        IndexDto indexDto = new IndexDto();

        // 表示平台登录，不是门店登录
        if (storeId <= 0) {
            storeId = null;
        }

        // 注册用户数
        indexDto.setUserCount(yxUserService.count());

        // 订单总数
        indexDto.setOrderCount(tShareOrderMapper.sumOrderCount(storeId));

        // 实付订单总金额
        indexDto.setPriceCount(tShareOrderMapper.sumRealOrderMoney(storeId));

        // 充值金额
        indexDto.setRechargeOrderMoney(mtOrderMapper.sumRechargeOrderMoney(storeId));

        // 充值次数
        indexDto.setSumRechargeCount(mtOrderMapper.sumRechargeCount(storeId));

        //indexDto.setMemberCount(tMembersUserService.count());

        //indexDto.setMemberCount(0);

        Date today = DateUtil.beginOfDay(new Date());
        Date yesterday = DateUtil.beginOfDay(DateUtil.yesterday());
        Date nowMonth = DateUtil.beginOfMonth(new Date());
        Date lastWeek = DateUtil.beginOfDay(DateUtil.lastWeek());

        //今日订单数
        LambdaQueryWrapper<TShareOrder> wrapperOne = new LambdaQueryWrapper<>();
        List<Integer> orderStatusIds = new ArrayList<>();
        orderStatusIds.add(1);
        orderStatusIds.add(2);
        orderStatusIds.add(4);
        wrapperOne.ge(TShareOrder::getPayTime, today)
                .in(TShareOrder::getOrderStatus, orderStatusIds);

        if (storeId != null) {
            wrapperOne.eq(TShareOrder::getStoreId, storeId);
        }
        indexDto.setTodayCount(tShareOrderMapper.selectCount(wrapperOne));

        //昨日订单数
        LambdaQueryWrapper<TShareOrder> wrapperTwo = new LambdaQueryWrapper<>();
        wrapperTwo.lt(TShareOrder::getPayTime, today)
                .ge(TShareOrder::getPayTime, yesterday)
                .in(TShareOrder::getOrderStatus, orderStatusIds);

        if (storeId != null) {
            wrapperTwo.eq(TShareOrder::getStoreId, storeId);
        }
        indexDto.setProCount(tShareOrderMapper.selectCount(wrapperTwo));

        //上周订单数
        LambdaQueryWrapper<TShareOrder> wrapperLastWeek = new LambdaQueryWrapper<>();
        wrapperLastWeek
                .lt(TShareOrder::getPayTime, today)
                .ge(TShareOrder::getPayTime, lastWeek)
                .in(TShareOrder::getOrderStatus, orderStatusIds);

        if (storeId != null) {
            wrapperLastWeek.eq(TShareOrder::getStoreId, storeId);
        }
        indexDto.setLastWeekCount(tShareOrderMapper.selectCount(wrapperLastWeek));

        //本月订单数
        LambdaQueryWrapper<TShareOrder> wrapperThree = new LambdaQueryWrapper<>();
        wrapperThree
                .ge(TShareOrder::getPayTime, nowMonth)
                .in(TShareOrder::getOrderStatus, orderStatusIds);

        if (storeId != null) {
            wrapperThree.eq(TShareOrder::getStoreId, storeId);
        }
        indexDto.setMonthCount(tShareOrderMapper.selectCount(wrapperThree));

        return indexDto;
    }
}
